package actions.saturnMusic.dialog;

import bean.TemplateType;
import utils.CommonUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ActivityTemplateDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JFormattedTextField inputParentName;
    private JFormattedTextField inputLayoutName;
    private JRadioButton fragmentType;
    private JRadioButton activityType;
    private transient ICallBack mCallBack;

    public static void runWithCallBack(ICallBack callBack) {
        ActivityTemplateDialog dialog = new ActivityTemplateDialog();
        dialog.mCallBack = callBack;
        dialog.setTitle("生成MVP模板");
        dialog.pack();
        dialog.setLocation(MouseInfo.getPointerInfo().getLocation());
        dialog.setVisible(true);
    }

    private ActivityTemplateDialog() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(e -> onOK());
        buttonCancel.addActionListener(e -> onCancel());
        activityType.addActionListener(e -> {
            if (fragmentType.isSelected()) {
                fragmentType.setSelected(false);
            }
        });

        fragmentType.addActionListener(e -> {
            if (activityType.isSelected()) {
                activityType.setSelected(false);
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0)
                , JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        TemplateType type;
        if (activityType.isSelected()) {
            type = TemplateType.ACTIVITY;
        } else if (fragmentType.isSelected()) {
            type = TemplateType.FRAGMENT;
        } else {
            type = null;
        }
        if (type == null) {
            CommonUtil.showMessage("错误的选择！", "至少选择一个类型 activity/fragment!");
            return;
        }
        // add your code here
        if (mCallBack != null) {
            mCallBack.onOk(inputParentName.getText(), inputLayoutName.getText(), type);
        }
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    public static void main(String[] args) {
        ActivityTemplateDialog.runWithCallBack((inputParentName1, layoutName, type) -> {
            Logger.getGlobal().log(Level.ALL, inputParentName1 + layoutName + type);
        });
    }

    public interface ICallBack {
        void onOk(String inputParentName, String layoutName, TemplateType type);
    }
}
